'use strict'
const utils = require('utility')

module.exports = app => {
  class Main extends app.Service {
    async list(query) {
      let { uid, time } = query
      time = utils.YYYYMMDD(new Date(time))
      let article_ids = await this.app.mysql.select('article', {
        columns: [ 'id' ],
        where: { uid },
      })
      article_ids = article_ids.map(a => a.id)
      const c_sql = `SELECT content, floor_num, article_id, article_type, from_uid, create_at, u.name, u.realname FROM comment 
        LEFT JOIN user u ON u.id = from_uid WHERE article_id in (?) AND create_at >= DATE(?) `
      const comments = await this.app.mysql.query(c_sql, [ article_ids.join(), time ])

      const r_sql = `SELECT r.content, r.comment_id, r.reply_id, r.reply_type, r.from_uid, r.create_at, u.name, u.realname, c.article_id FROM reply r
      LEFT JOIN user u ON u.id = from_uid LEFT JOIN comment c ON c.id = comment_id  WHERE to_uid = ? AND r.create_at >= DATE(?)`
      const replies = await this.app.mysql.query(r_sql, [ uid, time ])
      return { comments, replies, article_ids };
    }

  }

  return Main;
};
